package com.tring.ysyn.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.tring.ysyn.entity.SysUser;
import com.tring.ysyn.model.dto.user.UserDTO;
import com.tring.ysyn.service.ISysUserService;
import com.tring.ysyn.util.Result;
import com.tring.ysyn.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author tring
 * @since 2022-10-31
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    /**
     * 修改密码
     * @param dto 用户
     * @return
     */
    @PostMapping(value = "/updatePwd")
    public Result updatePwd(@RequestBody UserDTO dto) {
        userService.updatePwd(dto);
        return Result.success("修改密碼成功");
    }

    /**
     * 冻结用户或解冻用户
     * @param id 用户id
     * @return
     */
    @GetMapping(value = "/disable/{id}")
    public Result disable(@PathVariable("id") String id) {
        userService.disableOrAble(id);
        return Result.success("冻结成功");
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return
     */
    @GetMapping(value = "/del/{id}")
    public Result del(@PathVariable("id") String id) {
        StpUtil.kickout(id);
        userService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 踢下线
     *
     * @param id 用户id
     * @return
     */
    @GetMapping(value = "/kickout/{id}")
    public Result kickout(@PathVariable("id") String id) {
        SysUser user = UserUtils.getCurrentUser();
        if (user != null) {
            StpUtil.kickout(id);
            return Result.success(user.getUserName() + "踢下线成功");
        }
        return Result.success("踢下线失败");
    }
}

